package sample.job;

import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import sample.job.start.CopyStart;

import static org.quartz.JobBuilder.newJob;

/**
 * @author 杨佳颖
 * @Data 2022-03-21 9:19
 **/
public class CopyJob {
    private static Scheduler scheduler;

    public static void job(String dbType,
                           String dburl,
                           String dbuser,
                           String dbpass,
                           String dbport,
                           String dbname,
                           String dbsql,
                           String dbfield,
                           String savepath,
                           String xcs,
                           String serverIP,
                           String replacementLocation,
                           String replaceContent,
                           String cronTime,
                           TextArea log,
                           ProgressBar jindutiao,
                           Label jindutiaowenzi,
                           String cron) throws SchedulerException {
        //创建调度器
        //将具体的作业类（RamJob）绑定到调度任务详情中
        //创建触发器
        scheduler = new StdSchedulerFactory().getScheduler();

        JobDetail jobDetail = newJob(CopyStart.class)
                // 任务名，任务组
                .withIdentity("copy")
                .build();

        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        jobDataMap.put("dbType",dbType);
        jobDataMap.put("dburl",dburl);
        jobDataMap.put("dbuser",dbuser);
        jobDataMap.put("dbpass",dbpass);
        jobDataMap.put("dbport",dbport);
        jobDataMap.put("dbname",dbname);
        jobDataMap.put("dbsql",dbsql);
        jobDataMap.put("dbfield",dbfield);
        jobDataMap.put("savepath",savepath);
        jobDataMap.put("xcs",xcs);
        jobDataMap.put("serverIP",serverIP);
        jobDataMap.put("replacementLocation",replacementLocation);
        jobDataMap.put("replaceContent",replaceContent);
        jobDataMap.put("cronTime",cronTime);
        jobDataMap.put("log",log);
        jobDataMap.put("jindutiao",jindutiao);
        jobDataMap.put("jindutiaowenzi",jindutiaowenzi);

        Trigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
        //将触发器以及调度任务详情绑定到调度器上
        scheduler.scheduleJob(jobDetail, trigger);
        //启动调度器

        scheduler.start();
    }

    public static boolean stop() {
        try {
            if (scheduler != null) {
                scheduler.shutdown(true);
            }
            return true;
        } catch (SchedulerException e) {
            return false;
        }
    }
}
